<template>
  <div class="cpwd">
    <div class="form-container">
      <el-form ref="passwordForm" :model="passwordForm" :rules="passwordRules" label-width="100px"
        class="change-password-form">
        <el-form-item label="原密码" prop="oldPassword">
          <el-input type="password" v-model="passwordForm.oldPassword" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input type="password" v-model="passwordForm.newPassword" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input type="password" v-model="passwordForm.confirmPassword" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="changePassword">确认修改</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'ChangePassword',

    data() {
      return {
        teacherId: '',
        passwordForm: {
          oldPassword: '',
          newPassword: '',
          confirmPassword: ''
        },
        passwordRules: {
          oldPassword: [
            { required: true, message: '请输入原密码', trigger: 'blur' }
          ],
          newPassword: [
            { required: true, message: '请输入新密码', trigger: 'blur' },
            { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
          ],
          confirmPassword: [
            { required: true, message: '请再次输入新密码', trigger: 'blur' },
            { validator: this.validateConfirmPassword, trigger: 'blur' }
          ]
        }
      };
    },

    mounted() {
      this.teacherId = this.$cookies.get('user').teacherId; // 在组件挂载时从路由参数中获取 ID
      console.log(this.teacherId)
    },

    methods: {
      changePassword() {
        this.$refs.passwordForm.validate(valid => {
          if (valid) {
            this.$axios({
              url: '/api/changePwdTea',
              method: 'post',
              data: {
                ...this.passwordForm,
                id: this.teacherId
              }
            }).then((result) => {
              console.log(result.data)
              if (result.data.code == 0) {
                this.$message({
                  message: "密码修改成功，请重新登录",
                  type: "success",
                  showClose: true, // 显示关闭按钮
                  onClose: () => {
                    this.$router.push({ path: '/', query: { tel: this.$store.state.userTel } });
                  }
                });


              }
              if (result.data.code == 1) {
                this.$message.error("原密码不匹配")
                this.resetForm
              }

            }).catch((err) => {

            });
          }
        });
      },
      resetForm() {
        this.$refs.passwordForm.resetFields();
      },
      validateConfirmPassword(rule, value, callback) {
        if (value !== this.passwordForm.newPassword) {
          callback(new Error('两次输入的密码不一致'));
        } else {
          callback();
        }
      }
    },
  };
</script>

<style scoped>
  .cpwd {
    display: flex;
    justify-content: center;
    align-items: center;
    height: calc(100vh - 100px);
  }

  .form-container {
    max-width: 400px;
  }

  .change-password-form {
    margin-top: 20px;
  }
</style>